/*
author:  陈楚城
create:  2019.4.28
description：学生宿舍信息模块的controller层
*/
package edu.uni.dormitory.controller;

import com.github.pagehelper.PageInfo;
import edu.uni.auth.bean.User;
import edu.uni.auth.service.AuthService;
import edu.uni.bean.Result;
import edu.uni.bean.ResultType;
import edu.uni.dormitory.bean.LiveArea;
import edu.uni.dormitory.bean.LiveBed;
import edu.uni.dormitory.bean.StudentBed;
import edu.uni.dormitory.dto.Query;
import edu.uni.dormitory.globalbean.ClassVo;
import edu.uni.dormitory.globalbean.StudentBedClassVo;
import edu.uni.dormitory.globalbean.StudentBedVo;
import edu.uni.dormitory.service.AreaService;
import edu.uni.dormitory.service.BedService;
import edu.uni.dormitory.service.StudentBedService;
import edu.uni.example.bean.Product;
import edu.uni.userBaseInfo1.bean.Employee;
import edu.uni.userBaseInfo1.bean.Student;
import edu.uni.userBaseInfo1.service.EmployeeService;
import edu.uni.userBaseInfo1.service.StudentService;
import edu.uni.utils.RedisCache;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

import static edu.uni.bean.ResultType.Failed;
import static edu.uni.bean.ResultType.Success;

/**
 * @author 陈楚城
 * @create 2019.05.02
 * @function 学生床位
 */
@Api(description = "学生住宿信息模块")
@Controller
@RequestMapping("json/dorm/studentbed")
public class StudentBedController {
    @Autowired
    private StudentBedService studentBedService;
    @Autowired
    private BedService bedService;
    @Autowired
    private AreaService areaService;

    @Autowired
    private RedisCache cache;
    @Autowired
    private AuthService authService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private EmployeeService employeeService;
    /**
     * 内部类，专门用来管理每个方法所对应缓存的名称。
     */
    static class CacheNameHelper{
        // dorm_studentbed_{学生id}
        private static final String Receive_CacheNamePrefix = "dorm_studentbed_";
        // dorm_studentbed_Alist_{宿舍区id}
        private static final String List_A_CacheNamePrefix = "dorm_studentbed_Alist_";
        // dorm_studentbed_Elist_{某辅导员id}
        private static final String List_E_CacheNamePrefix = "dorm_studentbed_Elist_";
        // dorm_studentbed_listByAid_{宿舍区id}_{页码}
        private static final String ListByAid_CacheNamePrefix = "dorm_studentbed_listByAid_";
        // dorm_studentbed_listByEid_{某辅导员id}_{页码}
        private static final String ListByEid_CacheNamePrefix = "dorm_studentbed_listByEid_";
    }

    /**
     * 根据学生ID查询某个学生的住宿信息
     */

    @ApiOperation(value = "根据学生id获取住宿信息", notes = "未测试")
    //@ApiImplicitParam(name = "studentId", value = "学生id", required = true, dataType = "long", paramType = "path")
    @GetMapping()
    public void getStudentBedBystudentId( HttpSession session,HttpServletResponse response) throws IOException {
        response.setContentType("application/json;charset=utf-8");
        //String cacheName =CacheNameHelper.Receive_CacheNamePrefix+studentId;
        //String json = cache.get(cacheName);
        //获取登录学生的id
        User user = authService.getUser();
        List<Student> students=studentService.selectByUserId(user.getId());
        long studentId=students.get(0).getId();
        System.out.println(studentId);

        String json =null;
        if(json == null){
            StudentBedVo studentBedVo=studentBedService.selectStudentBedBySid(studentId);
            if(studentBedVo.getUserSex()==0){
                studentBedVo.setSex("女");
            }else
                studentBedVo.setSex("男");
            if(studentBedVo.getStatus()==0){
                studentBedVo.setState("待审");
            }else if(studentBedVo.getStatus()==1){
                studentBedVo.setState("同意");
            }else if(studentBedVo.getStatus()==-1){
                studentBedVo.setState("不同意");
            }
            List<StudentBedVo> list=new ArrayList<>();;
            list.add(studentBedVo);
            json = Result.build(ResultType.Success).appendData("list", list).convertIntoJSON();
            if(studentBedVo != null){
                //cache.set(cacheName, json);
            }
        }
        response.getWriter().write(json);
       // cache.deleteByPaterm(CacheNameHelper.Receive_CacheNamePrefix + "*");
       // return Result.build(Success);
    }
    /**
     *根据房间id查询这个宿舍住的学生信息
     *
     */
    @ApiOperation(value = "根据房间id查询这个宿舍住的学生信息", notes = "未测试")
    //@ApiImplicitParam(name = "id", value = "宿舍区id", required = true, dataType = "Integer", paramType = "path")
    @GetMapping("RoomstudentBedlist/{roomId}")
    public void getStudentBedList(@PathVariable long roomId, HttpSession session,HttpServletResponse response) throws IOException{
        response.setContentType("application/json;charset=utf-8");
        //String cacheName =CacheNameHelper.List_A_CacheNamePrefix +areaId;
        // String json = cache.get(cacheName);
        String json=null;
        if(json == null){
            List<StudentBedVo> list=studentBedService.selectStudentBedAll();
            for(StudentBedVo studentBedVo:list){
                if(studentBedVo.getUserSex()==0){
                    studentBedVo.setSex("女");
                }else
                    studentBedVo.setSex("男");
                if(studentBedVo.getStatus()==0){
                    studentBedVo.setState("待审");
                }else if(studentBedVo.getStatus()==1){
                    studentBedVo.setState("同意");
                }else if(studentBedVo.getStatus()==-1){
                    studentBedVo.setState("不同意");
                }
            }
            List<StudentBedVo> list1=new ArrayList<>();
            for(StudentBedVo studentBedVo:list){
                if (studentBedVo.getLiveRoomId()==roomId){
                    list1.add(studentBedVo);
                }
            }
            json = Result.build(ResultType.Success).appendData("list", list1).convertIntoJSON();
            if(list1 != null){
                // cache.set(cacheName, json);
            }
        }
        response.getWriter().write(json);
        //return Result.build(Success);
    }
    /**
     *根据宿舍区获取学生住宿信息
     */
    @ApiOperation(value = "根据宿舍区id获取学生住宿信息", notes = "未测试")
    //@ApiImplicitParam(name = "id", value = "宿舍区id", required = true, dataType = "Integer", paramType = "path")
    @GetMapping("arealist/{areaId}")
    public void getStudentBedAreaList(@PathVariable long areaId, HttpSession session,HttpServletResponse response) throws IOException{
        response.setContentType("application/json;charset=utf-8");
        //String cacheName =CacheNameHelper.List_A_CacheNamePrefix +areaId;
       // String json = cache.get(cacheName);
        String json=null;
        if(json == null){
            List<StudentBedVo> list=studentBedService.selectStudentBedByAid(areaId);
            for(StudentBedVo studentBedVo:list){
                if(studentBedVo.getUserSex()==0){
                    studentBedVo.setSex("女");
                }else
                    studentBedVo.setSex("男");
                if(studentBedVo.getStatus()==0){
                    studentBedVo.setState("待审");
                }else if(studentBedVo.getStatus()==1){
                    studentBedVo.setState("同意");
                }else if(studentBedVo.getStatus()==-1){
                    studentBedVo.setState("不同意");
                }
            }
            json = Result.build(ResultType.Success).appendData("list", list).convertIntoJSON();
            if(list != null){
               // cache.set(cacheName, json);
            }

        }
        response.getWriter().write(json);
        //return Result.build(Success);
    }
    /**
     * 根据班级获取某学院的学生住宿信息
     */
    @ApiOperation(value = "根据班级id获取某学院的学生住宿信息", notes = "未测试")
    //@ApiImplicitParam(name = "id", value = "宿舍区id", required = true, dataType = "Integer", paramType = "path")
    @GetMapping("classlist/{classId}")
    public void getStudentBedListAcademyList(@PathVariable long classId, HttpSession session,HttpServletResponse response) throws IOException{
        response.setContentType("application/json;charset=utf-8");
        //String cacheName =CacheNameHelper.List_E_CacheNamePrefix +employeeId;
       // String json = cache.get(cacheName);
        //long loginId = (long) session.getAttribute("loginId");//session中取得登录人id
        String json=null;
        if(json == null){
            List<StudentBedClassVo> list=studentBedService.selectCid(classId);
            for(StudentBedClassVo StudentBedClassVo:list){
                if(StudentBedClassVo.getUserSex()==0){
                    StudentBedClassVo.setSex("女");
                }else
                    StudentBedClassVo.setSex("男");
                if(StudentBedClassVo.getStatus()==0){
                    StudentBedClassVo.setState("待审");
                }else if(StudentBedClassVo.getStatus()==1){
                    StudentBedClassVo.setState("同意");
                }else if(StudentBedClassVo.getStatus()==-1){
                    StudentBedClassVo.setState("不同意");
                }
            }
            json = Result.build(ResultType.Success).appendData("list", list).convertIntoJSON();
            if(list != null){
                //cache.set(cacheName, json);
            }

        }
        response.getWriter().write(json);
        //return Result.build(Success);

    }
    /**
     *宿管中心获取所有宿舍区学生住宿信息
     */
    @ApiOperation(value = "宿管中心获取所有宿舍区学生住宿信息", notes = "未测试")
    //@ApiImplicitParam(name = "id", value = "宿舍区id", required = true, dataType = "Integer", paramType = "path")
    @GetMapping("studentBedlist")
    public void getStudentBedList( HttpSession session,HttpServletResponse response) throws IOException{
        response.setContentType("application/json;charset=utf-8");
        //String cacheName =CacheNameHelper.List_A_CacheNamePrefix +areaId;
        // String json = cache.get(cacheName);
        String json=null;
        if(json == null){
            List<StudentBedVo> list=studentBedService.selectStudentBedAll();
            for(StudentBedVo studentBedVo:list){
                if(studentBedVo.getUserSex()==0){
                    studentBedVo.setSex("女");
                }else
                    studentBedVo.setSex("男");
                if(studentBedVo.getStatus()==0){
                    studentBedVo.setState("待审");
                }else if(studentBedVo.getStatus()==1){
                    studentBedVo.setState("同意");
                }else if(studentBedVo.getStatus()==-1){
                    studentBedVo.setState("不同意");
                }
            }
            json = Result.build(ResultType.Success).appendData("list", list).convertIntoJSON();
            if(list != null){
                // cache.set(cacheName, json);
            }
        }
        response.getWriter().write(json);
        //return Result.build(Success);
    }


    //根据辅导员id查询这个学院的班级信息
    @ApiOperation(value = "根据辅导员id查询这个学院的班级信息,辅导员id未知", notes = "未测试")
    @GetMapping("studentClassList")
    public void studentClassList( HttpSession session,HttpServletResponse response) throws IOException {
        response.setContentType("application/json;charset=utf-8");
        //获取登录学生的id
        User user = authService.getUser();
        List<Employee> employees=employeeService.selectByUserId(user.getId());
        long employeeId=employees.get(0).getId();
        String json=null;
        if(json == null) {
          List<ClassVo> list=studentBedService.selectclassbyEid(employeeId);
          for(ClassVo classVo: list){
              //studentBedService.selectclasspersonNum(classVo.getId());
              classVo.setPerpleNum(studentBedService.selectclasspersonNum(classVo.getId()));
              List<StudentBedClassVo> studentBedClassVos=studentBedService.selectCid(classVo.getId());
              classVo.setNum(studentBedClassVos.size());
          }
          json = Result.build(ResultType.Success).appendData("list", list).convertIntoJSON();
            if(list != null){
                // cache.set(cacheName, json);
            }
        }
        response.getWriter().write(json);
    }

    @ApiOperation(value = "高级筛选", notes = "未测试")
    //@ApiImplicitParam(name = "studentId", value = "学生id", required = true, dataType = "long", paramType = "path")
    @PostMapping("selective")
    public void selective( @RequestBody  StudentBedClassVo studentBedClassVo,HttpSession session,HttpServletResponse response) throws IOException {
        response.setContentType("application/json;charset=utf-8");
        //String cacheName =CacheNameHelper.Receive_CacheNamePrefix+studentId;
       // System.out.println(studentBedClassVo);
       // System.out.println(classVo.getClassName());
        System.out.println(studentBedClassVo.getUserName());
        studentBedClassVo.setStatus((byte)1);
        String json =null;
        if(json == null){
            List<StudentBedClassVo> list=studentBedService.selective(studentBedClassVo);
            for(StudentBedClassVo StudentBedClassVo:list){
                if(StudentBedClassVo.getUserSex()==0){
                    StudentBedClassVo.setSex("女");
                }else
                    StudentBedClassVo.setSex("男");
                if(StudentBedClassVo.getStatus()==0){
                    StudentBedClassVo.setState("待审");
                }else if(StudentBedClassVo.getStatus()==1){
                    StudentBedClassVo.setState("同意");
                }else if(StudentBedClassVo.getStatus()==-1){
                    StudentBedClassVo.setState("不同意");
                }
            }

            json = Result.build(ResultType.Success).appendData("list", list).convertIntoJSON();
            if(list != null){
                //cache.set(cacheName, json);
            }
        }
        response.getWriter().write(json);
        // cache.deleteByPaterm(CacheNameHelper.Receive_CacheNamePrefix + "*");
        // return Result.build(Success);
    }


















    /**
     * 根据宿舍区id和页码来分页查询学生宿舍信息
     */
    @ApiOperation(value = "根据宿舍区id和页码来分页查询学生宿舍信息", notes = "未测试")
    @GetMapping(value = "listByAid/{areaId}/{pageNum}")
    public void listByAid(@PathVariable long areaId,
                          @PathVariable Integer pageNum,
                          HttpServletResponse response) throws IOException {
        response.setContentType("application/json;charset=utf-8");
        //String cacheName = CacheNameHelper.ListByAid_CacheNamePrefix + areaId + "_" + pageNum;
        //String json = cache.get(cacheName);
        String json=null;
        if(json == null){
            PageInfo<StudentBedVo> pageInfo =studentBedService.selectPageByAreaId(pageNum,areaId);
            json = Result.build(ResultType.Success).appendData("pageInfo", pageInfo).convertIntoJSON();
            if(pageInfo != null) {
                //cache.set(cacheName, json);
            }
        }
        response.getWriter().write(json);
        // return  Result.build(Success);
    }
    /**
     * 根据辅导员id和页码来分页查询学生宿舍信息
     */
    @ApiOperation(value = "根据某辅导员id和页码来分页查询学生宿舍信息", notes = "未测试")
    @GetMapping(value = "listByEid/{employeeId}/{pageNum}")
    public void listByEid(@PathVariable long employeeId,
                          @PathVariable Integer pageNum,
                          HttpServletResponse response) throws IOException {
        response.setContentType("application/json;charset=utf-8");
        //String cacheName = CacheNameHelper.ListByEid_CacheNamePrefix + employeeId + "_" + pageNum;
        // String json = cache.get(cacheName);
        String json=null;
        if(json == null){
            PageInfo<StudentBedClassVo> pageInfo =studentBedService.selectPageByEmployeeId(pageNum,employeeId);
            json = Result.build(ResultType.Success).appendData("pageInfo", pageInfo).convertIntoJSON();
            if(pageInfo != null) {
                //cache.set(cacheName, json);
            }
        }
        response.getWriter().write(json);
        // return  Result.build(Success);
    }
    /**
     * 分页获取宿管中心获取所有宿舍区学生住宿信息
     */
    @ApiOperation(value = "分页获取宿管中心获取所有宿舍区学生住宿信息", notes = "未测试")
    @GetMapping(value = "studentBedlist/{pageNum}")
    public void getStudentBedListByPage(@PathVariable Integer pageNum,
                          HttpServletResponse response) throws IOException {
        response.setContentType("application/json;charset=utf-8");
        //String cacheName = CacheNameHelper.ListByAid_CacheNamePrefix + areaId + "_" + pageNum;
        //String json = cache.get(cacheName);
        String json=null;
        if(json == null){
            PageInfo<StudentBedVo> pageInfo =studentBedService.selectStudentBedAllByPage(pageNum);
            json = Result.build(ResultType.Success).appendData("pageInfo", pageInfo).convertIntoJSON();
            if(pageInfo != null) {
                //cache.set(cacheName, json);
            }
        }
        response.getWriter().write(json);
        // return  Result.build(Success);
    }

}
